<?php

class Xzsdsplus_Costpriceplus_Block_Tab_Form extends Mage_Adminhtml_Block_Widget_Form {

	public function getSaveUrl() {
		return Mage::getUrl('costpriceplus/index/save', array('_secure'=>true));
	}
	
	protected function _prepareForm() {

		$form = new Varien_Data_Form();

		$fieldset = $form->addFieldset('banner_form', array('legend'=>Mage::helper('costpriceplus')->__('Banner information')));
		
		$outputFormat = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM);
		
		$fieldset->addField('title', 'text', array(
			'name'      => 'title',
			'label'     => Mage::helper('costpriceplus')->__('Title'),
			'class'     => 'required-entry',
			'required'  => true,
			'maxlength' => 250,
		));
		
		$fieldset->addField('linkto', 'text', array(
			'name'      => 'linkto',
			'label'     => Mage::helper('costpriceplus')->__('weblink'),
			'maxlength' => 510,
		));
		
		$fieldset->addField('status', 'select', array(
			'name'  => 'status',
			'label' => Mage::helper('costpriceplus')->__('Status'),
			'title' => Mage::helper('costpriceplus')->__('Status'),
			'class' => '',
			'values' => array(
					array('label'=>'on','value'=>1),
					array('label'=>'off','value'=>0),
				),
		));
		
		$categories = array();
		$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name');
		foreach ($collection as $cat) {
			$categories[] = ( array(
				'label' => (string)$cat->getName(),
				'value' => $cat->getEntityId()
				));
		}
		
		$banner_id = Mage::registry('costpriceplus')->getBanner_id();
		$cats = array();
		if ($banner_id) {
			$db = Mage::getSingleton('core/resource')->getConnection('core_write');
			$result = $db->query(" SELECT category_id FROM costpriceplus_category WHERE banner_id = {$banner_id}");
			while ($cat = $result->fetch()) {
				$cats[] = $cat['category_id'];
			}
		}
		Mage::registry('costpriceplus')->setCategory_id($cats);
		
		$fieldset->addField('category_id', 'multiselect', array(
				'name'      => 'cats[]',
				'label'     => Mage::helper('costpriceplus')->__('Category'),
				'title'     => Mage::helper('costpriceplus')->__('Category'),
				'required'  => true,
				'style'    => 'height:100px',
				'value' => $cats,
				'values'    => $categories,
				//'note'      => Mage::helper('costpriceplus')->__('Root catalog is show on all Page'),
		));
		
		$fieldset->addField('image', 'image', array(
				'name'      => 'image',
				'label'     => Mage::helper('costpriceplus')->__('Image'),
				'title'     => Mage::helper('costpriceplus')->__('Image'),
				//'required'  => true,
		));
		
		$fieldset->addField('start_at','date',array(
			'name'      => 'start_at',
			'time'      => true,
			'format'    => $outputFormat,
			'label'     => Mage::helper('costpriceplus')->__('Banner Date Start'),
			'image'     => $this->getSkinUrl('images/grid-cal.gif')
		));
	
		$fieldset->addField('end_at','date',array(
			'name'      => 'end_at',
			'time'      => true,
			'format'    => $outputFormat,
			'label'     => Mage::helper('costpriceplus')->__('Queue Date End'),
			'image'     => $this->getSkinUrl('images/grid-cal.gif')
		));
		
		$fieldset->addField('remark','textarea',array(
			'name'      => 'remark',
			'label'     => Mage::helper('costpriceplus')->__('Remark'),
			'title'     => Mage::helper('costpriceplus')->__('Remark'),
			'note'      => Mage::helper('costpriceplus')->__('Maximum length is 510'),
			'maxlength' => 510
		));
		
		if ( Mage::getSingleton('adminhtml/session')->getBannerData() ) {
			$form->setValues(Mage::getSingleton('adminhtml/session')->getBannerData());
			Mage::getSingleton('adminhtml/session')->setBannerData(null);
		} else if ( Mage::registry('costpriceplus') ) {
			$form->setValues(Mage::registry('costpriceplus')->getData());
		}
		$this->setForm($form);
		return parent::_prepareForm();
	}
	
	protected function _getAdditionalElementTypes() {
		return array(
			'image' => Mage::getConfig()->getBlockClassName('adminhtml/catalog_category_helper_image') 
		);    
	}
}